package com.japp.base;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.japp.tools.page.Page;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction<T> extends ActionSupport implements Params,ServletRequestAware,ServletResponseAware {
	
	private static final long serialVersionUID = 7726443241882159665L;
	protected HttpSession session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected PrintWriter writer = null;
	protected String[] keys = null;
	protected String key = null;
	protected String json_val = "";
	protected String result;	//输出参数
	protected String obj_date = "";
	protected String obj_cdate = "";
	
	protected Page<T> page = new Page<T>();
	/**每页显示几条*/
	@SuppressWarnings("unused")
	private Integer size = 10;
	/**总条数*/
	@SuppressWarnings("unused")
	private Integer total = 0;
	/**当前页*/
	@SuppressWarnings("unused")
	private Integer currentPage = 0; 
	
	protected void wPrint(String str){
		writer = getWriter();
		writer.flush();
		writer.write(str);
		writer.close();
	}
	
	
	
	/******** GETTER AND SETTER ********/
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
		response.setCharacterEncoding("UTF-8");
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
		session = getSession();
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public PrintWriter getWriter() {
		
		try {
			if(writer==null){
				response.setCharacterEncoding("UTF-8");
				return response.getWriter();
			}else{
				return writer;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public void setWriter(PrintWriter writer) {
		this.writer = writer;
	}
	public Page<T> getPage() {
		return page;
	}
	public void setPage(Page<T> page) {
		this.page = page;
	}
	public HttpSession getSession() {
		return request.getSession();
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String getJson_val() {
		return json_val;
	}
	public void setJson_val(String json_val) {
		this.json_val = json_val;
	}
	public Integer getSize() {
		return page.getSize();
	}
	public void setSize(Integer size) {
		page.setSize(size);
		this.size = size;
	}
	public Integer getTotal() {
		return page.getTotal();
	}
	public void setTotal(Integer total) {
		page.setTotal(total);
		this.total = total;
	}
	public Integer getCurrentPage() {
		return page.getCurrentPage();
	}
	public void setCurrentPage(Integer currentPage) {
		page.setCurrentPage(currentPage);
		this.currentPage = currentPage;
	}
	/**
	 * @return the keys
	 */
	public String[] getKeys() {
		return keys;
	}
	/**
	 * @param keys the keys to set
	 */
	public void setKeys(String[] keys) {
		this.keys = keys;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getObj_date() {
		return obj_date;
	}
	public void setObj_date(String obj_date) {
		this.obj_date = obj_date;
	}
	public String getObj_cdate() {
		return obj_cdate;
	}
	public void setObj_cdate(String obj_cdate) {
		this.obj_cdate = obj_cdate;
	}
}
